var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nav_list: [],
        goods:[],
        img_url:c.img_url,
        slip:0,
        select:-1,
        select2: 0,
        show:false,
        list_title: [
            { title: '推荐', select: 0 },
            { title: '销量', select: 1 },
            { title: '最新', select: 2 },
            { title: '价格', select: 3 },
        ],
        Sorting: true,
        Sorting1: true,
        list_tab:0,
        order:1,
        cat_id:"",
        classify_list:[],
        patchlist:[],
        imgurl: [],
        share_point:0,
        share_start_content:"",
        share_end_content:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getCat();
        this.getSlide();
        this.getactivity();
        this.getGoodsList();
        //utils.checkLogin();
        this.getShareInfo();
    },
    getCat: function () {
        var that = this;
        var data = {
            url: "/wsgoods/Goodscat/lists",
            parent_id: 0,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    data[index].pic = c.img_url + data[index].pic;
                }
                that.setData({
                    nav_list: data
                });
            } else {

            }
        });
    },
    Tab_nav:function(e){
        let k = e.currentTarget.dataset.index;
        page = 1;
        let id = e.currentTarget.dataset.id;
        var that = this;
        var data = {
            url: "/wsgoods/Goodscat/lists",
            parent_id: id,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    data[index].pic = c.img_url + data[index].pic;
                }
                that.setData({
                    classify_list: data
                });
            } else {

            }
        });
        this.setData({
            select: k,
            cat_id: id
        })
        wx.pageScrollTo({
            scrollTop: 0
        })
        this.getGoodsList();
    },
    Tab_nav2: function (e) {
        let k = e.currentTarget.dataset.index;
        this.setData({
            select2: k
        })
    },
    unfold:function(){
        this.setData({
            show:true
        })
    },
    pack_up: function () {
        this.setData({
            show: false
        })
    },
    list_tab: function (e) {
        let key = e.currentTarget.dataset.select;
        page = 1;
        this.setData({
            list_tab: key
        });
        if (key == 0) {//综合
            this.setData({
                order: 1
            })
        } else if (key == 2) {//最新
            this.setData({
                order: 7
            })
        } else if (key == 3) {//价格排序
            this.setData({
                Sorting1: false,
                Sorting: !this.data.Sorting
            });
            if (!this.data.Sorting) {
                this.setData({
                    order: 4
                })
            } else {
                this.setData({
                    order: 5
                })
            }
        } else {
            this.setData({
                order: 2
            })
        }
        this.getGoodsList();
    },
    //上拉加载
    onReachBottom: function () {
        page++;
        this.getGoodsList();
    },
    /**
     * 获取商品列表
     */
    getGoodsList:function(){
        var that = this;
        var data = {
            url: "/wsgoods/goods/lists",
            keyword: "",
          cat_id: this.data.cat_id ? this.data.cat_id : '',
            rec_id: "",
            page: page,
            limit: 20,
            order: this.data.order
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    var points_price = data[index]['price']-data[index]['point'];
                    data[index]['points_price'] = points_price.toFixed(2);
                }
                if (page == 1) {
                    that.setData({
                        goods: data
                    });
                } else {
                    that.setData({
                        goods: that.data.goods.concat(data)
                    });
                }
            } else {

            }
        });
    },
    /**
     * 搜索内容
     */
    editKeyword: function (e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    /**
     * 搜索商品
     */
    searchGoods: function () {
        var keyword = this.data.keyword;
        wx.navigateTo({
            url: '/pages/shop/goodslist/index?keyword=' + keyword,
        })
    },
    getactivity:function(){
      var that = this;
      var data = {
        url: "/zfcom/patch/lists",
        uuid: "",
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          var data = res.data;
          that.setData({
            patchlist: data
          });
        } else {

        }
      });
    },
    /**
     * 幻灯
     */
    getSlide: function () {
      var that = this;
      var data = {
        url: "/zfcom/slide/lists",
        cat_id: 16,
        page: 1,
        limit: 1,
        keyword: ""
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          var data = res.data;
          for (var index in data) {
            data[index].pic = c.img_url + data[index].pic;
          }
          that.setData({
            imgurl: data
          });
        } else {

        }
      });
    },
    //分类图片错误处理
    errCategoryPic: function (e) {
        var index = e.target.dataset.index;
        var nav_list = this.data.nav_list;
        nav_list[index].pic = "/static/images/shop/default.png";
        this.setData({
            nav_list: nav_list
        });
    },
    errPic: function (e) {
        var index = e.target.dataset.index;
        var classify_list = this.data.classify_list;
        classify_list[index].pic = "/static/images/shop/default.png";
        this.setData({
            classify_list: classify_list
        });
    },
    //分享
    onShareAppMessage: function (res) {
      var that = this;
      if (res.from === "button") {
        var id = res.target.dataset.id;
        var name = res.target.dataset.name;
        var pic = res.target.dataset.pic;
        var description = res.target.dataset.description;
        var price = res.target.dataset.price;
        var point = res.target.dataset.point;
        var points_price = price-point;
        return {
          title: that.data.share_start_content + name + " ￥" + points_price.toFixed(2) + that.data.share_end_content,
          path: '/pages/shop/goodsdetail/index?id=' + id + "&invitecode=" + wx.getStorageSync("userinfo").invitecode,
          imageUrl: pic,
          success: function (res) {
            wx.getSystemInfo({
              success: function (d) {
                var token = wx.getStorageSync("userinfo").wap_token;
                var data = {
                  url: "/wsgoods/usershare/add",
                  token: token,
                  type: 0,
                  id: id
                };
                utils.request(data).then(function (res) {
                  if (res.code == 0) {

                  }
                });
              }
            })
          },
        }
      }
    },
    jump: function (res){
      utils.r("/pages/shop/goodsdetail/index?id=" + res.currentTarget.dataset.id);
    },
    //分享信息
    getShareInfo: function () {
      var that = this;
      var data = {
        url: "/zfcom/com/deploy",
        uuid: "share_point,share_start_content,share_end_content",
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          that.setData({
            share_point:res.data.share_point,
            share_start_content: res.data.share_start_content,
            share_end_content: res.data.share_end_content,
          });
        }
      });
    },
})
